
#include "src/intersect_error/line_arc_error.h"

void ArcSegmentIntersection::RunTests(int numtests){

    std::vector<double> errors;
    double max_error = 0.0;
    double sum_error = 0.0;

    double num{};



    for(int i = 0; i < numtests; ++i){

        GenerateRandomArc();

        Point p1 = GenerateRandomPoint();
        Point p2 = ReflectPoint(p1);

        std::vector<Point> intersections = intersect(p1,p2);

        if(intersections.size() == 1) {//tangency

            double error = CalculateError(intersections[0]);

            errors.push_back(error);

            //if(error > 1e-12) num++;

        }else if(intersections.size() == 2){//intersect

            double dist1 = CalculateError(intersections[0]);
            double dist2 = CalculateError(intersections[1]);

            double distance = sqrt((intersections[0].x - intersections[1].x)*(intersections[0].x - intersections[1].x) +
                                   (intersections[0].y - intersections[1].y)*(intersections[0].y - intersections[1].y));

            if(distance < EPSILON){

                errors.push_back(dist1);

                //if(dist1 > 1e-12) num++;

            }else{

                errors.push_back(std::min(dist1,dist2));

                //if(std::min(dist1,dist2) > 1e-12) num++;

            }
        }

        if(!errors.empty()){

            double current_errors = errors.back();

            max_error = std::max(max_error,current_errors);

            sum_error += current_errors;

            if(max_error> 1e-12) num++;
        }

//        std::cout << "Test " << i + 1 << ": \n";

        //std::cout << std::fixed << std::setprecision(2);
//        std::cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ")\n";
//        std::cout << "Arc cneter: (" << center.x << ", " << center.y << "),radius: " << radius << std::endl;
//        std::cout << "Arc start angle: " << start_angle << ",endangle: " << end_angle << std::endl;

    }

//    for(double error : errors){
//        std::cout << std::scientific << error << " ";
//    }

    std::cout << std::endl;

    std::cout << "\nAvgrage Error: " << sum_error / errors.size() << std::endl;
    std::cout << "\nMax Error: " << max_error << std::endl;
    std::cout << std::fixed << std::setprecision(2) <<"\nThe probability of error exceeding 1e-12:" << (double)num / errors.size()  << "%" << std::endl;



}

int main(){

    ArcSegmentIntersection asi;
    asi.RunTests(1000000);

    return 0;
}
